<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
	<xsl:template match="doc">
		<xsl:variable name="sort_category" select="substring-before($sort, ' ')"/>
		<xsl:variable name="regularized_sort">
			<xsl:choose>
				<xsl:when test="$sort_category = 'unitid_display'">Accession</xsl:when>
				<xsl:when test="$sort_category = 'collection_facet'">Collection</xsl:when>
				<xsl:when test="$sort_category = 'corpname_facet'">Political Entity</xsl:when>
				<xsl:when test="$sort_category = 'deity_facet'">Deity</xsl:when>
				<xsl:when test="$sort_category = 'denomination_facet'">Denomination</xsl:when>
				<xsl:when test="$sort_category = 'department_facet'">Department</xsl:when>
				<xsl:when test="$sort_category = 'dimensions_sint'">Dimensions</xsl:when>
				<xsl:when test="$sort_category = 'famname_facet'">Dynasty</xsl:when>
				<xsl:when test="$sort_category = 'institution_facet'">Institution</xsl:when>
				<xsl:when test="$sort_category = 'material_facet'">Material</xsl:when>
				<xsl:when test="$sort_category = 'persname_facet'">Name</xsl:when>
				<xsl:when test="$sort_category = 'obv_leg_display'">Obverse</xsl:when>
				<xsl:when test="$sort_category = 'reference_display'">Reference</xsl:when>
				<xsl:when test="$sort_category = 'rev_leg_display'">Reverse</xsl:when>
				<xsl:when test="$sort_category = 'script_facet'">Script</xsl:when>
				<xsl:when test="$sort_category = 'style_facet'">Style</xsl:when>
				<xsl:when test="$sort_category = 'technique_facet'">Technique</xsl:when>
				<xsl:when test="$sort_category = 'weight_sint'">Weight</xsl:when>
				<xsl:otherwise>Undefined Category</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		
		<xsl:variable name="class">
			<xsl:choose>
				<xsl:when test="position() mod 2 = 0">result_div_even</xsl:when>
				<xsl:otherwise>result_div_odd</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<div class="{$class}">
			<xsl:choose>
				<xsl:when test="$mode='compare'">
					<xsl:choose>
						<xsl:when test="str[@name='format_facet'] = 'coin'">
							<xsl:variable name="img_string">
								<xsl:choose>
									<xsl:when test="$image='reverse'">
										<xsl:text>screen_rev</xsl:text>
									</xsl:when>
									<xsl:otherwise>
										<xsl:text>screen_obv</xsl:text>
									</xsl:otherwise>
								</xsl:choose>
							</xsl:variable>
							<div style="text-align:center;">
								<img src="{str[@name=$img_string]}"
									style="max-height:320px; min-height:320px;"/>
							</div>
						</xsl:when>
						<xsl:otherwise>
							<div style="text-align:center;">
								<img src="{arr[@name='screen_image']/str[1]}"
									style="max-height:320px; min-height:320px;"/>
							</div>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:otherwise>
					<xsl:choose>
						<xsl:when test="str[@name='format_facet'] = 'coin'">
							<xsl:if
								test="string(str[@name='thumb_obv']) or string(str[@name='thumb_rev']) or string(str[@name='thumb_com'])">
								<div style="float:right;">
									<xsl:if test="string(str[@name='thumb_obv'])">
										<a class="jqueryLightbox" href="{str[@name='screen_obv']}"
											title="Obverse of {str[@name='unittitle_display']}">
											<img src="{str[@name='thumb_obv']}"/>
										</a>
									</xsl:if>
									<xsl:if test="string(str[@name='thumb_obv'])">
										<a class="jqueryLightbox" href="{str[@name='screen_rev']}"
											title="Reverse of {str[@name='unittitle_display']}">
											<img src="{str[@name='thumb_rev']}"/>
										</a>
									</xsl:if>
									<xsl:if test="string(str[@name='thumb_com'])">
										<a class="jqueryLightbox" href="{str[@name='screen_com']}"
											title="Reverse of {str[@name='unitid_display']}, {str[@name='unittitle_display']}">
											<img src="{str[@name='thumb_com']}"/>
										</a>
									</xsl:if>
								</div>
							</xsl:if>
						</xsl:when>
						<xsl:otherwise>
							<xsl:if test="arr[@name='thumb_image']/str">
								<div style="float:right;">
									<a class="jqueryLightbox"
										href="{arr[@name='screen_image']/str[1]}"
										title="Image of {str[@name='unittitle_display']}">
										<img src="{arr[@name='thumb_image']/str[1]}"/>
									</a>
								</div>
							</xsl:if>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:otherwise>
			</xsl:choose>

			<div style="margin-left:50px;width:400px;">
				<dl>
					<div class="result_header">
						<xsl:choose>
							<xsl:when test="$mode = 'compare'">
								<a
									href="display/{str[@name='source_meta']}/{str[@name='id']}?mode=compare&amp;q={$q}&amp;start={$start}&amp;image={$image}&amp;side={$side}"
									class="compare">
									<xsl:value-of select="str[@name='unittitle_display']"/>
								</a>
							</xsl:when>
							<xsl:otherwise>
								<a href="display/{str[@name='source_meta']}/{str[@name='id']}">
									<xsl:value-of select="str[@name='unittitle_display']"/>
								</a>
							</xsl:otherwise>
						</xsl:choose>
					</div>
					<div>
						<dt>
							<b>Date: </b>
						</dt>
						<dd>
							<xsl:choose>
								<xsl:when test="string(str[@name='unitdate_display'])">
									<xsl:value-of select="str[@name='unitdate_display']"/>
								</xsl:when>
								<xsl:otherwise>
									<xsl:text>[Unknown]</xsl:text>
								</xsl:otherwise>
							</xsl:choose>
						</dd>
					</div>
					<div>
						<dt>
							<b>Origination: </b>
						</dt>
						<dd>
							<xsl:choose>
								<xsl:when test="string(str[@name='origination_display'])">
									<xsl:value-of select="str[@name='origination_display']"/>
								</xsl:when>
								<xsl:otherwise>
									<xsl:text>[Unknown]</xsl:text>
								</xsl:otherwise>
							</xsl:choose>
						</dd>
					</div>
					
					<xsl:if
						test="string($sort) and not(contains($sort_category, 'origination')) and not(contains($sort_category, 'year'))">
						<xsl:choose>
							<xsl:when test="contains($sort, '_sint')">
								<xsl:if test="arr[@name=$sort_category]/float">
									<div>
										<dt>
											<b>
												<xsl:value-of select="$regularized_sort"/>
												<xsl:text>: </xsl:text>
											</b>
										</dt>
										
										<dd>
											<xsl:for-each select="arr[@name=$sort_category]/float">
												<xsl:sort order="descending"/>
												<xsl:value-of select="."/>
												<xsl:choose>
													<xsl:when test="$sort_category='dimensions_sint'">
														<xsl:text> mm</xsl:text>
													</xsl:when>
													<xsl:when test="$sort_category='weight_sint'">
														<xsl:text> grams</xsl:text>
													</xsl:when>
												</xsl:choose>
												<xsl:if test="not(position() = last())">
													<xsl:text>, </xsl:text>
												</xsl:if>
											</xsl:for-each>
										</dd>
									</div>
								</xsl:if>
							</xsl:when>
							<xsl:when
								test="contains($sort, '_facet') or contains($sort, 'reference_display')">
								<xsl:choose>
									<xsl:when test="matches($sort, 'format_facet')">
										<div>
											<dt>
												<b>Format: </b>
											</dt>
											<dd>
												<xsl:value-of
													select="concat(upper-case(substring(str[@name='format_facet'], 1, 1)), substring(str[@name='format_facet'], 2))"
												/>
											</dd>
										</div>
									</xsl:when>
									<xsl:otherwise>
										<xsl:if test="arr[@name=$sort_category]/str">
											<div>
												<dt>
													<b>
														<xsl:value-of select="$regularized_sort"/>
														<xsl:text>: </xsl:text>
													</b>
												</dt>
												<dd>
													<xsl:for-each
														select="arr[@name=$sort_category]/str">
														<xsl:sort order="descending"/>
														<xsl:value-of select="."/>
														<xsl:if test="not(position() = last())">
															<xsl:text>, </xsl:text>
														</xsl:if>
													</xsl:for-each>
												</dd>
											</div>
										</xsl:if>
									</xsl:otherwise>
								</xsl:choose>
							</xsl:when>
							<xsl:otherwise>
								<xsl:if test="str[@name=$sort_category]">
									<div>
										<dt>
											<b>
												<xsl:value-of select="$regularized_sort"/>
												<xsl:text>: </xsl:text>
											</b>
										</dt>
										<dd>
											<xsl:value-of
												select="substring(str[@name=$sort_category], 1, 25)"/>
											<xsl:if
												test="string-length(str[@name=$sort_category]) &gt; 25">
												<xsl:text>...</xsl:text>
											</xsl:if>
										</dd>
									</div>
								</xsl:if>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:if>
				</dl>
			</div>
		</div>
	</xsl:template>

	<xsl:template name="paging">
		<xsl:variable name="start_var" as="xs:integer">
			<xsl:choose>
				<xsl:when test="string($start)">
					<xsl:value-of select="$start"/>
				</xsl:when>
				<xsl:otherwise>0</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		
		<xsl:variable name="numFound" as="xs:integer">
			<xsl:value-of select="//result[@name='response']/@numFound"/>
		</xsl:variable>
		
		<xsl:variable name="next">
			<xsl:value-of select="$start_var+$rows"/>
		</xsl:variable>
		
		<xsl:variable name="previous">
			<xsl:choose>
				<xsl:when test="$start_var &gt;= $rows">
					<xsl:value-of select="$start_var - $rows"/>
				</xsl:when>
				<xsl:otherwise>0</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		
		<xsl:variable name="current" select="$start_var div $rows + 1"/>
		<xsl:variable name="total" select="ceiling($numFound div $rows)"/>
		
		<div class="paging_div">
			<div style="float:left;">
				<xsl:text>Displaying records </xsl:text>
				<b>
					<xsl:value-of select="$start_var + 1"/>
				</b>
				<xsl:text> to </xsl:text>
				<xsl:choose>
					<xsl:when test="$numFound &gt; ($start_var + $rows)">
						<b>
							<xsl:value-of select="$start_var + $rows"/>
						</b>
					</xsl:when>
					<xsl:otherwise>
						<b>
							<xsl:value-of select="$numFound"/>
						</b>
					</xsl:otherwise>
				</xsl:choose>
				<xsl:text> of </xsl:text>
				<b>
					<xsl:value-of select="$numFound"/>
				</b>
				<xsl:text> total results.</xsl:text>
			</div>
			
			<!-- paging functionality -->
			<div style="float:right;">
				<xsl:choose>
					<xsl:when test="$start_var &gt;= $rows">
						<xsl:choose>
							<xsl:when test="string($sort)">
								<a class="pagingBtn"
									href="?q={encode-for-uri($q)}&amp;start={$previous}&amp;sort={$sort}"
									>«Previous</a>
							</xsl:when>
							<xsl:otherwise>
								<a class="pagingBtn" href="?q={encode-for-uri($q)}&amp;start={$previous}"
									>«Previous</a>
							</xsl:otherwise>
						</xsl:choose>
						
					</xsl:when>
					<xsl:otherwise>
						<span class="pagingSep">«Previous</span>
					</xsl:otherwise>
				</xsl:choose>
				
				<!-- always display links to the first two pages -->
				<xsl:if test="$start_var div $rows &gt;= 3">
					<xsl:choose>
						<xsl:when test="string($sort)">
							<a class="pagingBtn" href="?q={encode-for-uri($q)}&amp;start=0&amp;sort={$sort}">
								<xsl:text>1</xsl:text>
							</a>
						</xsl:when>
						<xsl:otherwise>
							<a class="pagingBtn" href="?q={encode-for-uri($q)}&amp;start=0">
								<xsl:text>1</xsl:text>
							</a>
						</xsl:otherwise>
					</xsl:choose>
					
				</xsl:if>
				<xsl:if test="$start_var div $rows &gt;= 4">
					<xsl:choose>
						<xsl:when test="string($sort)">
							<a class="pagingBtn" href="?q={encode-for-uri($q)}&amp;start={$rows}&amp;sort={$sort}">
								<xsl:text>2</xsl:text>
							</a>
						</xsl:when>
						<xsl:otherwise>
							<a class="pagingBtn" href="?q={encode-for-uri($q)}&amp;start={$rows}">
								<xsl:text>2</xsl:text>
							</a>
						</xsl:otherwise>
					</xsl:choose>
					
				</xsl:if>
				
				<!-- display only if you are on page 6 or greater -->
				<xsl:if test="$start_var div $rows &gt;= 5">
					<span class="pagingSep">...</span>
				</xsl:if>
				
				<!-- always display links to the previous two pages -->
				<xsl:if test="$start_var div $rows &gt;= 2">
					<xsl:choose>
						<xsl:when test="string($sort)">
							<a class="pagingBtn"
								href="?q={encode-for-uri($q)}&amp;start={$start_var - ($rows * 2)}&amp;sort={$sort}">
								<xsl:value-of select="($start_var div $rows) -1"/>
							</a>
						</xsl:when>
						<xsl:otherwise>
							<a class="pagingBtn" href="?q={encode-for-uri($q)}&amp;start={$start_var - ($rows * 2)}">
								<xsl:value-of select="($start_var div $rows) -1"/>
							</a>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:if>
				<xsl:if test="$start_var div $rows &gt;= 1">
					<xsl:choose>
						<xsl:when test="string($sort)">
							<a class="pagingBtn"
								href="?q={encode-for-uri($q)}&amp;start={$start_var - $rows}&amp;sort={$sort}">
								<xsl:value-of select="$start_var div $rows"/>
							</a>
						</xsl:when>
						<xsl:otherwise>
							<a class="pagingBtn" href="?q={encode-for-uri($q)}&amp;start={$start_var - $rows}">
								<xsl:value-of select="$start_var div $rows"/>
							</a>
						</xsl:otherwise>
					</xsl:choose>
					
				</xsl:if>
				
				<span class="pagingBtn">
					<b>
						<xsl:value-of select="$current"/>
					</b>
				</span>
				
				<!-- next two pages -->
				<xsl:if test="($start_var div $rows) + 1 &lt; $total">
					<xsl:choose>
						<xsl:when test="string($sort)">
							<a class="pagingBtn"
								href="?q={encode-for-uri($q)}&amp;start={$start_var + $rows}&amp;sort={$sort}">
								<xsl:value-of select="($start_var div $rows) +2"/>
							</a>
						</xsl:when>
						<xsl:otherwise>
							<a class="pagingBtn" href="?q={encode-for-uri($q)}&amp;start={$start_var + $rows}">
								<xsl:value-of select="($start_var div $rows) +2"/>
							</a>
						</xsl:otherwise>
					</xsl:choose>
					
				</xsl:if>
				<xsl:if test="($start_var div $rows) + 2 &lt; $total">
					<xsl:choose>
						<xsl:when test="string($sort)">
							<a class="pagingBtn"
								href="?q={encode-for-uri($q)}&amp;start={$start_var + ($rows * 2)}&amp;sort={$sort}">
								<xsl:value-of select="($start_var div $rows) +3"/>
							</a>
						</xsl:when>
						<xsl:otherwise>
							<a class="pagingBtn" href="?q={encode-for-uri($q)}&amp;start={$start_var + ($rows * 2)}">
								<xsl:value-of select="($start_var div $rows) +3"/>
							</a>
						</xsl:otherwise>
					</xsl:choose>
					
				</xsl:if>
				<xsl:if test="$start_var div $rows &lt;= $total - 6">
					<span class="pagingSep">...</span>
				</xsl:if>
				
				<!-- last two pages -->
				<xsl:if test="$start_var div $rows &lt;= $total - 5">
					<xsl:choose>
						<xsl:when test="string($sort)">
							<a class="pagingBtn"
								href="?q={encode-for-uri($q)}&amp;start={($total * $rows) - ($rows * 2)}&amp;sort={$sort}">
								<xsl:value-of select="$total - 1"/>
							</a>
						</xsl:when>
						<xsl:otherwise>
							<a class="pagingBtn"
								href="?q={encode-for-uri($q)}&amp;start={($total * $rows) - ($rows * 2)}">
								<xsl:value-of select="$total - 1"/>
							</a>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:if>
				<xsl:if test="$start_var div $rows &lt;= $total - 4">
					<xsl:choose>
						<xsl:when test="string($sort)">
							<a class="pagingBtn"
								href="?q={encode-for-uri($q)}&amp;start={($total * $rows) - $rows}&amp;sort={$sort}">
								<xsl:value-of select="$total"/>
							</a>
						</xsl:when>
						<xsl:otherwise>
							<a class="pagingBtn" href="?q={encode-for-uri($q)}&amp;start={($total * $rows) - $rows}">
								<xsl:value-of select="$total"/>
							</a>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:if>
				
				<xsl:choose>
					<xsl:when test="$numFound - $start_var &gt; $rows">
						<xsl:choose>
							<xsl:when test="string($sort)">
								<a class="pagingBtn"
									href="?q={encode-for-uri($q)}&amp;start={$next}&amp;sort={$sort}">Next»</a>
							</xsl:when>
							<xsl:otherwise>
								<a class="pagingBtn" href="?q={encode-for-uri($q)}&amp;start={$next}">Next»</a>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:when>
					<xsl:otherwise>
						<span class="pagingSep">Next»</span>
					</xsl:otherwise>
				</xsl:choose>
			</div>
		</div>
	</xsl:template>

	<xsl:template name="sort">
		<div class="sort_div">
			<form class="sortForm" action="results">
				<select class="sortForm_categories" style="width:150px;">
					<option value="null">Select from list...</option>
					<option value="unitid_display">Accession Number</option>
					<option value="collection_facet">Collection</option>
					<option value="deity_facet">Deity</option>
					<option value="denomination_facet">Denomination</option>
					<option value="department_facet">Department</option>
					<option value="dimensions_sint">Dimensions</option>
					<option value="famname_facet">Dynasty</option>
					<option value="format_facet">Format</option>
					<option value="institution_facet">Institution</option>
					<option value="material_facet">Material</option>
					<option value="persname_facet">Name</option>
					<option value="obv_leg_display">Obverse Legend</option>
					<option value="origination_display">Origination</option>
					<option value="corpname_facet">Political Entity</option>
					<option value="reference_display">Reference</option>
					<option value="rev_leg_display">Reverse Legend</option>
					<option value="script_facet">Script</option>
					<option value="style_facet">Style</option>
					<option value="technique_facet">Technique</option>
					<option value="weight_sint">Weight</option>
					<option value="year_sint">Year</option>
				</select>
				<select class="sortForm_order" style="width:100px;">
					<option value="asc">Ascending</option>
					<option value="desc">Descending</option>
				</select>
				<input type="hidden" name="q" value="{$q}"/>
				<input type="hidden" name="sort" value="" class="sort_param"/>
				<input class="sort_button" type="submit" value="Sort Results" disabled="disabled"/>
			</form>
		</div>
	</xsl:template>

	<xsl:template name="quick_search">
		<div class="quick_search">
			<form action="results" method="GET">
				<input type="text" id="qs_text" width="40"/>
				<input type="hidden" name="q" id="qs_query"/>
				<input id="qs_button" type="submit" value="Go"/>
			</form>
		</div>
	</xsl:template>

	<xsl:template match="lst[@name='facet_fields']">
		<xsl:for-each select="lst[descendant::int]">
			<xsl:variable name="facet" select="@name"/>
			<xsl:variable name="name">
				<xsl:choose>
					<xsl:when test="@name = 'artist_facet'">Artist</xsl:when>
					<xsl:when test="@name = 'city_facet'">City</xsl:when>					
					<xsl:when test="@name = 'collection_facet'">Collection</xsl:when>
					<xsl:when test="@name = 'color_facet'">Color</xsl:when>
					<xsl:when test="@name = 'corpname_facet'">Political Entity</xsl:when>
					<xsl:when test="@name = 'deity_facet'">Deity</xsl:when>
					<xsl:when test="@name = 'denomination_facet'">Denomination</xsl:when>
					<xsl:when test="@name = 'department_facet'">Department</xsl:when>
					<xsl:when test="@name = 'famname_facet'">Dynasty</xsl:when>
					<xsl:when test="@name = 'engraver_facet'">Engraver</xsl:when>
					<xsl:when test="@name = 'format_facet'">Format</xsl:when>
					<xsl:when test="@name = 'institution_facet'">Institution</xsl:when>
					<xsl:when test="@name = 'issuer_facet'">Issuer</xsl:when>
					<xsl:when test="@name = 'persname_facet'">Name</xsl:when>
					<xsl:when test="@name = 'maker_facet'">Maker</xsl:when>
					<xsl:when test="@name = 'manufacture_facet'">Manufacture</xsl:when>
					<xsl:when test="@name = 'material_facet'">Material</xsl:when>
					<xsl:when test="@name = 'region_facet'">Region</xsl:when>
					<xsl:when test="@name = 'state_facet'">State</xsl:when>
					<xsl:when test="@name = 'script_facet'">Script</xsl:when>
					<xsl:when test="@name = 'style_facet'">Style</xsl:when>
					<xsl:when test="@name = 'technique_facet'">Technique</xsl:when>
					<xsl:when test="@name = 'subject_facet'">Subject</xsl:when>
					<xsl:when test="@name = 'subjectEvent_facet'">Subject--Event</xsl:when>
					<xsl:when test="@name = 'subjectIssuer_facet'">Subject--Issuer</xsl:when>
					<xsl:when test="@name = 'subjectPerson_facet'">Subject--Person</xsl:when>
					<xsl:when test="@name = 'subjectPlace_facet'">Subject--Place</xsl:when>
					<xsl:when test="@name = 'era_sint'">Century</xsl:when>
					<xsl:otherwise>Undefined Category</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>
			<div class="facet">
				<div class="facet_label" id="{@name}_link" label="{$q}">
					<span style="float:left;width:80%;">
						<xsl:value-of select="$name"/>
					</span>
					<img class="get_facets" src="images/plus.png" alt="Expand"/>
				</div>
				<ul class="facet_list" id="{@name}-list" style="display:none;"/>
			</div>
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="remove_facets">
		<div class="remove_facets">
			<xsl:for-each select="$tokenized_q">
				<xsl:variable name="val" select="."/>
				<xsl:variable name="new_query">
					<xsl:for-each select="$tokenized_q[not($val = .)]">
						<xsl:value-of select="."/>
						<xsl:if test="position() != last()">
							<xsl:text> AND </xsl:text>
						</xsl:if>
					</xsl:for-each>
				</xsl:variable>				

				<!--<xsl:value-of select="."/>-->
				<xsl:choose>
					<xsl:when test="contains(., ':') and not(. = '*:*')">
						<xsl:variable name="field" select="substring-before(., ':')"/>
						<xsl:variable name="name">
							<xsl:choose>
								<xsl:when test="$field = 'artist_facet'">Arist</xsl:when>
								<xsl:when test="$field = 'city_facet'">City</xsl:when>
								<xsl:when test="$field = 'collection_facet'">Collection</xsl:when>
								<xsl:when test="$field = 'corpname_facet'">Political
									Entity</xsl:when>
								<xsl:when test="$field = 'deity_facet'">Deity</xsl:when>
								<xsl:when test="$field = 'denomination_facet'"
									>Denomination</xsl:when>
								<xsl:when test="$field = 'department_facet'">Department</xsl:when>
								<xsl:when test="$field = 'famname_facet'">Dynasty</xsl:when>
								<xsl:when test="$field = 'engraver_facet'">Engraver</xsl:when>
								<xsl:when test="$field = 'format_facet'">Format</xsl:when>
								<xsl:when test="$field = 'institution_facet'">Institution</xsl:when>
								<xsl:when test="$field = 'issuer_facet'">Issuer</xsl:when>
								<xsl:when test="$field = 'persname_facet'">Name</xsl:when>
								<xsl:when test="$field = 'maker_facet'">Maker</xsl:when>
								<xsl:when test="$field = 'manufacture_facet'">Manufacture</xsl:when>
								<xsl:when test="$field = 'material_facet'">Material</xsl:when>
								<xsl:when test="$field = 'region_facet'">Region</xsl:when>
								<xsl:when test="$field = 'state_facet'">State</xsl:when>
								<xsl:when test="$field = 'script_facet'">Script</xsl:when>
								<xsl:when test="$field = 'style_facet'">Style</xsl:when>
								<xsl:when test="$field = 'technique_facet'">Technique</xsl:when>
								<xsl:when test="$field = 'subject_facet'">Subject</xsl:when>
								<xsl:when test="$field = 'subjectEvent_facet'">Subject--Event</xsl:when>
								<xsl:when test="$field = 'subjectIssuer_facet'">Subject--Issuer</xsl:when>
								<xsl:when test="$field = 'subjectPerson_facet'">Subject--Person</xsl:when>
								<xsl:when test="$field = 'subjectPlace_facet'">Subject--Place</xsl:when>
								<xsl:when test="$field = 'fulltext'">Keyword</xsl:when>
								<xsl:when test="$field = 'unitid_text'">Identification
									Number</xsl:when>
								<xsl:when test="$field = 'legend_text'">Legend</xsl:when>
								<xsl:when test="$field = 'iconography_text'">Iconography</xsl:when>
								<xsl:when test="$field = 'persname_text'">Name</xsl:when>
								<xsl:when test="$field = 'geogname_text'">Location</xsl:when>
								<xsl:when test="$field = 'weight_sint'">Weight</xsl:when>
								<xsl:when test="$field = 'year_sint'">Year</xsl:when>
								<xsl:when test="$field = 'era_sint'">Century</xsl:when>
								<xsl:when test="$field = 'dimensions_sint'">Dimensions</xsl:when>

							</xsl:choose>
						</xsl:variable>
						<xsl:variable name="term"
							select="replace(substring-after(., ':'), '&#x022;', '')"/>
						<b><xsl:value-of select="$name"/>: </b>
						<xsl:choose>
							<xsl:when test="$field = 'era_sint'">
								<xsl:call-template name="regularize_century">
									<xsl:with-param name="term" select="$term"/>
								</xsl:call-template>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="$term"/>
							</xsl:otherwise>
						</xsl:choose>
						<xsl:text> [</xsl:text>
						<a href="?q={if (string($new_query)) then encode-for-uri($new_query) else '*:*'}">X</a>
						<xsl:text>]</xsl:text>
						<br/>
					</xsl:when>
					<xsl:when test="not(contains(., ':'))">
						<b>Keyword: </b>
						<xsl:value-of select="."/>
						<br/>
					</xsl:when>
					<xsl:when test=". = '*:*'">
						<b>All Terms</b>
					</xsl:when>
				</xsl:choose>
			</xsl:for-each>
			<xsl:if test="string($tokenized_q[2])">
				<a href="results?q=*:*">Clear All Terms</a>
			</xsl:if>

			<!-- remove sort term -->
			<xsl:if test="string($sort)">
				<xsl:variable name="field" select="substring-before($sort, ' ')"/>
				<xsl:variable name="name">
					<xsl:choose>
						<xsl:when test="$field = 'unitid_display'">Accession Number</xsl:when>
						<xsl:when test="$field = 'collection_facet'">Collection</xsl:when>
						<xsl:when test="$field = 'deity_facet'">Deity</xsl:when>
						<xsl:when test="$field = 'denomination_facet'">Denomination</xsl:when>
						<xsl:when test="$field = 'department_facet'">Department</xsl:when>
						<xsl:when test="$field = 'dimensions_sint'">Dimensions</xsl:when>
						<xsl:when test="$field = 'famname_facet'">Dynasty</xsl:when>
						<xsl:when test="$field = 'format_facet'">Format</xsl:when>
						<xsl:when test="$field = 'institution_facet'">Institution</xsl:when>
						<xsl:when test="$field = 'material_facet'">Material</xsl:when>
						<xsl:when test="$field = 'persname_facet'">Name</xsl:when>
						<xsl:when test="$field = 'obv_leg_display'">Obverse Legend</xsl:when>
						<xsl:when test="$field = 'origination_display'">Origination</xsl:when>
						<xsl:when test="$field = 'corpname_facet'">Political Entity</xsl:when>
						<xsl:when test="$field = 'reference_display'">Reference</xsl:when>
						<xsl:when test="$field = 'rev_leg_display'">Reverse Legend</xsl:when>
						<xsl:when test="$field = 'script_facet'">Script</xsl:when>
						<xsl:when test="$field = 'style_facet'">Style</xsl:when>
						<xsl:when test="$field = 'technique_facet'">Technique</xsl:when>
						<xsl:when test="$field = 'weight_sint'">Weight</xsl:when>
						<xsl:when test="$field = 'year_sint'">Year</xsl:when>
					</xsl:choose>
				</xsl:variable>

				<xsl:variable name="order">
					<xsl:choose>
						<xsl:when test="substring-after($sort, ' ') = 'asc'">Acending</xsl:when>
						<xsl:when test="substring-after($sort, ' ') = 'desc'">Descending</xsl:when>
					</xsl:choose>
				</xsl:variable>

				<br/>
				<b>Sort Category: </b>
				<xsl:value-of select="$name"/>
				<xsl:text>, </xsl:text>
				<xsl:value-of select="$order"/>
				<xsl:text> [</xsl:text>
				<a href="results?q={$q}">X</a>
				<xsl:text>]</xsl:text>
			</xsl:if>
		</div>
	</xsl:template>

	<xsl:template name="regularize_century">
		<xsl:param name="term"/>
		<xsl:choose>
			<xsl:when test="$term = '-5'">
				<xsl:text>Fifth Century B.C.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '-4'">
				<xsl:text>Fourth Century B.C.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '-3'">
				<xsl:text>Third Century B.C.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '-2'">
				<xsl:text>Second Century B.C.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '-1'">
				<xsl:text>First Century B.C.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '1'">
				<xsl:text>First Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '2'">
				<xsl:text>Second Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '3'">
				<xsl:text>Third Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '4'">
				<xsl:text>Fourth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '5'">
				<xsl:text>Fifth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '6'">
				<xsl:text>Sixth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '7'">
				<xsl:text>Seventh Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '8'">
				<xsl:text>Eight Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '9'">
				<xsl:text>Nineth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '10'">
				<xsl:text>Tenth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '11'">
				<xsl:text>Eleventh Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '12'">
				<xsl:text>Twelfth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '13'">
				<xsl:text>Thirteenth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '14'">
				<xsl:text>Fourteenth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '15'">
				<xsl:text>Fifteenth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '16'">
				<xsl:text>Sixteenth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '17'">
				<xsl:text>Seventeenth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '18'">
				<xsl:text>Eighteenth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '19'">
				<xsl:text>Nineteenth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '20'">
				<xsl:text>Twentieth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '21'">
				<xsl:text>Twenty-First Century A.D.</xsl:text>
			</xsl:when>
			<xsl:otherwise>other</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="compare_paging">
		<xsl:variable name="start_var">
			<xsl:choose>
				<xsl:when test="string($start)">
					<xsl:value-of select="$start"/>
				</xsl:when>
				<xsl:otherwise>0</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:variable name="next">
			<xsl:value-of select="$start_var+$rows"/>
		</xsl:variable>

		<xsl:variable name="previous">
			<xsl:choose>
				<xsl:when test="$start_var &gt;= $rows">
					<xsl:value-of select="$start_var - $rows"/>
				</xsl:when>
				<xsl:otherwise>0</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<div style="width:100%;display:table;">
			<div style="float:left;"> Results found: <b>
					<xsl:value-of select="$numFound"/>
				</b>. Page: <b>
					<xsl:value-of select="$start_var div $rows + 1"/>
				</b> of <b>
					<xsl:value-of select="ceiling($numFound div $rows)"/>
				</b>
			</div>
			<div style="float:right;">
				<xsl:choose>
					<xsl:when test="$start_var &gt;= $rows">
						<a class="comparepagingBtn"
							href="compare_results?q={$q}&amp;start={$previous}&amp;image={$image}&amp;side={$side}&amp;mode=compare"
							>« Previous</a>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>« Previous</xsl:text>
					</xsl:otherwise>
				</xsl:choose> | <xsl:choose>
					<xsl:when test="$numFound - $start_var &gt; $rows">
						<a class="comparepagingBtn"
							href="compare_results?q={$q}&amp;start={$next}&amp;image={$image}&amp;side={$side}&amp;mode=compare"
							>Next »</a>

					</xsl:when>
					<xsl:otherwise>
						<xsl:text>Next »</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</div>
		</div>
	</xsl:template>

	<xsl:template name="search_options">
		<option value="fulltext" class="search_option" id="keyword_option">Keyword</option>
		<option value="unitid_text" class="search_option" id="unitid_option">Accession
			Number</option>
		<option value="era_sint" class="search_option" id="era_option">Century</option>
		<option value="collection_facet" class="search_option" id="collection_option"
			>Collection</option>
		<option value="deity_text" class="search_option" id="deity_option">Deity</option>
		<option value="denomination_facet" class="search_option" id="denomination_option"
			>Denomination</option>
		<option value="department_facet" class="search_option" id="department_option"
			>Department</option>
		<option value="dimensions_sint" class="search_option" id="dimensions_option"
			>Dimensions</option>
		<option value="famname_facet" class="search_option" id="famname_option">Dynasty</option>
		<option value="format_facet" class="search_option" id="famname_option">Format</option>
		<option value="geogname_text" class="search_option" id="geogname_option">Geographical
			Location</option>
		<option value="iconography_text" class="search_option" id="iconography_option"
			>Iconography</option>
		<option value="institution_facet" class="search_option" id="institution_option"
			>Institution</option>
		<option value="legend_text" class="search_option" id="legend_option">Legend</option>
		<option value="material_facet" class="search_option" id="material_option">Material</option>
		<option value="persname_text" class="search_option" id="persname_option">Name</option>
		<option value="corpname_facet" class="search_option" id="corpname_option">Political
			Entity</option>
		<option value="script_facet" class="search_option" id="script_option">Script</option>
		<option value="style_facet" class="search_option" id="script_option">Style</option>
		<option value="subject_facet" class="search_option" id="subject_option">Subject</option>
		<option value="technique_facet" class="search_option" id="script_option">Technique</option>
		<option value="weight_sint" class="search_option" id="weight_option">Weight</option>
		<option value="year_sint" class="search_option" id="year_option">Year</option>
	</xsl:template>
</xsl:stylesheet>
